<?php

namespace platform;

use app\datacenter\model\Shop;
use think\facade\Log;

/**
 * 零塑平台管理
 */
class LinSuPlatform
{
    //申请获得的masterId
    public string $masterId ;
    public string $key ;
    //请求二维码获取接口
    public string $post_url ;
    private $reUrl;

    /**
     * 构造方法
     */
    public function __construct()
    {
        //环境加载配置
        if (env('ENVIRONMENT') == 'dev') {
            //测试环境
            $this->masterId = "eaczoobxtl1jztwhmdulcanxu32fr0ug";
            $this->post_url = 'http://open.test.lingsulife.com/getToUrl';
            $this->key = 'q5gt3ps1xychhsefmoci6nn8hq5dpncd';
            $this->reUrl =env('ApiDomainV2') .'/scan_bag/wechatJump';
        } else {
            //正式环境
            $this->masterId = "eaczoobxtl1jztwhmdulcanxu32fr0ug";
            $this->post_url = 'http://open.test.lingsulife.com/getToUrl';
            $this->key = 'q5gt3ps1xychhsefmoci6nn8hq5dpncd';
            $this->reUrl =env('ApiDomainV2') .'/scan_bag/wechatJump';
        }
    }

    /**
     * 查询接口
     * @param array $user
     * @param string $mac_id
     * @param string $lsId
     * @return array
     */
    public function queryInterface(array $user,string $mac_id,string $lsId): array
    {
        //获取当前设备所在位置Code
        $RegionCode = $this->getRegionCode($mac_id);
        //请求参数
        $time = time();
        $post_data = [
            'time' => $time,
            'lsId' => $lsId,
            'reUrl'=>$this->reUrl,
            'masterId' => $this->masterId,
            'openId' => $user['uuid'],
            'nickname' => $user['nickname'],
            'headImgUrl' => $user['avatar'],
            'province'=>$RegionCode['province'],
            'city'=>$RegionCode['city'],
            'area'=>$RegionCode['area'],
            'EquipmentCode'=>$mac_id

        ];
        //安全签名规则
        $signature = $this->sign($post_data['masterId'], $time);
        $params_ready = [
            'json' => array_merge($post_data, ['sign' => $signature])
        ];
        $result = httpPost($this->post_url, $params_ready);
        Log::write($params_ready);
        Log::write($result);
        $content = json_decode($result['content'], true);

        //判断结果
        if (isset($content['code']) and $content['code'] == 200) {
            return $content['data'];
        }
        return [];
    }

    /**
     * 构建签名字符
     * @param int|string $masterId
     * @param string $time
     * @return string
     */
    public function sign(int|string $masterId, string $time): string
    {
        $str = $masterId.$this->key.$time;
        return md5($str);
    }

/**
 * 通过设备ID获取所在地区编码
 * @param string $mac_id
 * @return array
 */
public function getRegionCode(string $mac_id): array
{
    $data['province'] = '0';
    $data['city'] = '0';
    $data['area'] = '0';
    //获取当前设备的shop信息
    $ShopInfo = Shop::getShopInfo($mac_id);
    if ($ShopInfo) {
        $data['province'] = $ShopInfo['province_id'];
        $data['city'] = $ShopInfo['city_id'];
        $data['area'] = $ShopInfo['region_id'];
    }
    return $data;
}


}